using System.Collections.Generic;
using XNgine.ConsoleComponent.Commands;
using XNgine.ConsoleComponent.Exceptions;
using XNgine.DebugConsole.Commands;

namespace XNgine.DebugConsole
{
    static class CommandInterpreter
    {
        public static CommandResult Execute(string command, object[] args)
        {
            return CommandRegister.Handler(command).Execute(args);
        }

        public static CommandResult Interpret(string commandString)
        {
            Command command = ExtractCommand(commandString);
            string args = ExtractArgs(commandString);

            CommandResult result = command.Execute(args);

            return result;
        }

        private static Command ExtractCommand(string commandString)
        {
            if (string.IsNullOrEmpty(commandString))
                throw new EmptyCommandException();

            string[] split = commandString.Split(' ');
            string cmd = split.Length == 0 ? commandString : split[0];

            return CommandRegister.Handler(cmd);
        }

        private static string ExtractArgs(string commandString)
        {
            string args;

            int spaceIndex = commandString.IndexOf(' ');
            if (spaceIndex <= 0)
                return null;

            args = commandString.Substring(spaceIndex);

            return args;
        }
    }
}